//
//  HPCustomerInfoDataSource.m
//  HappyPenguin
//
//  Created by zhuangyihang on 1/10/16.
//  Copyright © 2016 zhuangyihang. All rights reserved.
//

#import "HPCustomerInfoDataSource.h"
#import "AppHelper.h"

@interface HPCustomerInfoDataSource()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation HPCustomerInfoDataSource

- (id)initWithTableView:(UITableView *)tableView{
    self = [super init];
    if (self) {
        self.tableView = tableView;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
    }
    return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.infoObject.anniversaries.count==0?1:2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.infoObject.anniversaries.count==0) {
        return [self.infoObject getUserAttributes].count;
    }else{
        if (section==0) {
            return [self.infoObject getUserAttributes].count;
        }else{
            return self.infoObject.anniversaries.count;
        }
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"HPCustomerInfoCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    
    if (indexPath.section==0) {
        VipUserInfoFieldObject *obj = [self.infoObject getUserAttributes][indexPath.row];
        cell.textLabel.text = obj.name;
        cell.detailTextLabel.text = obj.value;
    }else{
        VipUserInfoAnniversaryFieldObject *obj = self.infoObject.anniversaries[indexPath.row];
        cell.textLabel.text = obj.name;
        cell.detailTextLabel.text = [AppHelper getStringFromDate:obj.date withFormat:DateFormatFullDate];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return @"基本资料";
    }else{
        return @"纪念日";
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return CGFLOAT_MIN;
    }
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
@end
